package com.uam.utils.file.analysis;

import com.alibaba.fastjson.JSONObject;
import com.uam.common.exception.UamException;
import com.uam.common.file.FileToStringUtils;
import com.uam.utils.file.analysis.help.FileTypeEnums;
import com.uam.utils.file.analysis.help.GeoJsonInfo;
import com.uam.utils.file.analysis.help.RetAnalysisVo;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * @author heguanglong
 * <p>
 * 文件解析成geojson工具类 支持geojson shp kml
 */
public class File2GeoJsonUtil {

    /**
     * 原文件的存储地址
     *
     * @param fileUrl         文件地址
     * @param targetDirectory 生成文件目录
     * @return RetAnalysisVo
     */
    public static RetAnalysisVo analysis(String fileUrl, String targetDirectory, String fileName) throws Exception {
        if (StringUtils.isBlank(fileName) || StringUtils.isBlank(fileUrl) || StringUtils.isBlank(targetDirectory)) {
            throw UamException.paramError("请求参fileUrl，targetDirectory，fileName 为必传！");
        }
        String[] fName = fileName.split("\\.");
        String name = fName[fName.length - 1];
        int type = FileTypeEnums.getCode(name);
        RetAnalysisVo retAnalysisVo = FileTypeEnums.toGeoJsonImpl(type).fileToGeoJsonFile(fileUrl, targetDirectory, fileName);
        retAnalysisVo.setFileType(name.toUpperCase());
        return retAnalysisVo;
    }

    public static void main(String[] args) throws Exception {
        // geojson
        String fileUrl = "E:\\ownCode\\UMA\\数据\\扇区数据\\voronoi.geojson";
        String dataUrl = "C:\\Users\\Administrator\\Desktop\\01";
        String fileName = "voronoi.geojson";
        RetAnalysisVo retAnalysisVo = File2GeoJsonUtil.analysis(fileUrl, dataUrl, fileName);
        System.out.println("geojson:" + JSONObject.toJSONString(retAnalysisVo));

        //kml
        String kmUrl = "C:\\Users\\Administrator\\Desktop\\01\\中华人民共和国铁路.kml";
        dataUrl = "C:\\Users\\Administrator\\Desktop\\01";
        fileName = "中华人民共和国铁路.kml";
        RetAnalysisVo retAnalysisKml = File2GeoJsonUtil.analysis(kmUrl, dataUrl, fileName);
        System.out.println("kml:" + JSONObject.toJSONString(retAnalysisKml));


        //shp
        kmUrl = "C:\\Users\\Administrator\\Desktop\\01\\TSGG20230417105044_8\\唐山港过程提交第八次20230721\\暗堤.shp";
        dataUrl = "C:\\Users\\Administrator\\Desktop\\01";
        fileName = "暗堤.shp";
        RetAnalysisVo retAnalysisShp = File2GeoJsonUtil.analysis(kmUrl, dataUrl, fileName);
        System.out.println("shp:" + JSONObject.toJSONString(retAnalysisShp));
    }
}
